# Различные пути
SHARED_SRC=../sysshare/src
SHARED_BIN=../sysshare/bin
SRC=src
BIN=bin
GRAPH_DIR=$(BIN)/sys/fsm/graph

SMC_PATH=$(SHARED_BIN)/smc_5_1/Smc.jar
STATEMAP_LIB_PATH=$(SHARED_BIN)/smc_5_1/statemap.jar
JAVA_MAIL_PATHS=$(SHARED_BIN)/javamail-1.4.2/mail.jar:$(SHARED_BIN)/javamail-1.4.2/lib/smtp.jar:$(SHARED_BIN)/javamail-1.4.2/lib/pop3.jar:$(SHARED_BIN)/javamail-1.4.2/lib/mailapi.jar:$(SHARED_BIN)/javamail-1.4.2/lib/mail.jar
MYSQL_CONNECTOR_PATH=$(SHARED_BIN)/mysql-connector/mysql-connector-java-5.1.7-bin.jar

FSM_SRC=$(SRC)/sys/fsm
SMC_SRC=$(SRC)/sys/smc

# Ява компилятор и его параметры
JAVAC = javac
JAVAC_PARAMS = -classpath $(SRC):$(STATEMAP_LIB_PATH):$(JAVA_MAIL_PATHS):$(JSON_PATH):$(MYSQL_CONNECTOR_PATH) -d $(BIN)

# Виртуальная машина Ява и её параметры
JAVA=java
JAVA_PARAMS=-classpath .:../$(STATEMAP_LIB_PATH):../$(JAVA_MAIL_PATHS):../$(JSON_PATH):../$(MYSQL_CONNECTOR_PATH)

all: compileSMC compileJAVA

# Генерация java кода всех автоматов
$(FSM_SRC)/%Context.java: $(SMC_SRC)/%.sm
	java -jar $(SMC_PATH) -d $(FSM_SRC) -java $<

# Генерация dot-файлов, описывающих все автоматы
$(GRAPH_DIR)/%_sm.svg: $(GRAPH_DIR)/%_sm.dot
	dot -Tsvg $< -o $@

$(GRAPH_DIR)/%_sm.dia: $(GRAPH_DIR)/%_sm.dot
	dot -Tdia $< -o $@

$(GRAPH_DIR)/%_sm.dot: $(SMC_SRC)/%.sm
	java -jar $(SMC_PATH) -d $(GRAPH_DIR) -glevel 0 -graph $< 

# Генерация кода из общего кода и из кода отдельной системы
$(BIN)/sys/timer/%.class: $(SHARED_SRC)/sys/timer/%.java 
	cp $< $(SRC)/sys/timer/ && $(JAVAC) $(JAVAC_PARAMS) $<

$(BIN)/sys/logic/%.class: $(SHARED_SRC)/sys/logic/%.java 
	cp $< $(SRC)/sys/logic/ && $(JAVAC) $(JAVAC_PARAMS) $<

$(BIN)/sys/transport/%.class: $(SHARED_SRC)/sys/transport/%.java 
	cp $< $(SRC)/sys/transport/ && $(JAVAC) $(JAVAC_PARAMS) $<

$(BIN)/sys/message/%.class: $(SHARED_SRC)/sys/message/%.java 
	cp $< $(SRC)/sys/message/ && $(JAVAC) $(JAVAC_PARAMS) $<

$(BIN)/sys/database/DBWrapper.class: $(SHARED_SRC)/sys/database/DBWrapper.java
	cp $< $(SRC)/sys/database/ && $(JAVAC) $(JAVAC_PARAMS) $<

$(BIN)/sys/utils/%.class: $(SHARED_SRC)/sys/utils/%.java 
	cp $< $(SRC)/sys/utils/ && $(JAVAC) $(JAVAC_PARAMS) $<

$(BIN)/%.class: $(SRC)/%.java 
	$(JAVAC) $(JAVAC_PARAMS) $<

# Все файлы, описывающие конечные автоматы в формате SMC
compileSMC: $(addprefix $(FSM_SRC)/, AutomatContext.java)
	
# Все java-файлы
compileJAVA: $(addprefix $(BIN)/sys/utils/, ReadWriteTextFile.class) $(addprefix $(BIN)/sys/timer/,TimerClient.class Timer.class)  $(addprefix $(BIN)/sys/message/, UniMessage.class JSONMessage.class) $(addprefix $(BIN)/sys/transport/, MessageReceiverPOP3.class MessageSenderSMTP.class MessageReceiver.class MessageSender.class) $(addprefix $(BIN)/sys/database/, DBWrapper.class SysbDatabase.class) $(addprefix $(BIN)/sys/logic/, MessageChecker.class MessageCheckerClient.class) $(addprefix $(BIN)/sys/fsm/, Automat.class AutomatContext.class) $(addprefix $(BIN)/sys/, Main.class RequestManager.class)  $(addprefix $(GRAPH_DIR)/, Automat_sm.dia) $(addprefix $(BIN)/sys/decision/, DecisionSubsystem.class) 
#$(addprefix $(BIN)/sys/tests/automat/, Test1.class Test2.class Test3.class RunTests.class) $(addprefix $(BIN)/sys/tests/automatLogic/, RunTests.class Test1.class) $(addprefix $(BIN)/sys/tests/logic/, Test1.class Test2.class Test3.class RunTests.class)

run: all
	cd $(BIN) && $(JAVA) $(JAVA_PARAMS) sys.Main 

runLogicTests: all
	cd $(BIN) && $(JAVA) $(JAVA_PARAMS) sys.Main "LogicTests"

runAutomatTests: all
	cd $(BIN) && $(JAVA) $(JAVA_PARAMS) sys.Main "AutomatTests"

runAutomatLogicTests: all
	cd $(BIN) && $(JAVA) $(JAVA_PARAMS) sys.Main "AutomatLogicTests"
